package com.novellotus.iov.carvice.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.RxEinfos;
import com.novellotus.iov.carvice.utils.MyViewUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2017/12/23.
 */

public class HomePageEnterpriseAdapter extends RecyclerView.Adapter<HomePageEnterpriseAdapter.HomePageEnterpriseHolder> {

    private Context context;
    private List<RxEinfos> rxEinfosList;
    private EnterpriseClickInterface EnterpriseClickInterface;

    public HomePageEnterpriseAdapter(List<RxEinfos> rxEinfosList, EnterpriseClickInterface EnterpriseClickInterface) {
        this.rxEinfosList = rxEinfosList;
        this.EnterpriseClickInterface = EnterpriseClickInterface;
    }

    @Override
    public HomePageEnterpriseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_enterprise, null);
        return new HomePageEnterpriseHolder(view);
    }

    @Override
    public void onBindViewHolder(final HomePageEnterpriseHolder holder, final int position) {
        holder.tv_name.setText(rxEinfosList.get(position).getEname());
        holder.recyclerView.setLayoutManager(MyViewUtils.gridLayoutManager(context, 3));
        holder.recyclerView.setAdapter(new HomePageRoleAdapter(rxEinfosList.get(position).getRoles()));
        holder.rlt_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (rxEinfosList.get(position) != null) {
                    EnterpriseClickInterface.clickEnterprise(rxEinfosList.get(position), position);
                }
            }
        });
//        holder.recyclerView.setOnTouchListener(new View.OnTouchListener() {
//
//            @Override
//            public boolean onTouch(View view, MotionEvent motionEvent) {
//                return false;
//            }
//        });
        holder.recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    if (rxEinfosList.get(position) != null) {
                        EnterpriseClickInterface.clickEnterprise(rxEinfosList.get(position), position);
                    }
                }
                return false;
            }
        });
//        holder.recyclerView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (rxEinfosList.get(position) != null) {
//                    EnterpriseClickInterface.clickEnterprise(rxEinfosList.get(position), position);
//                }
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return rxEinfosList.size();
    }

    class HomePageEnterpriseHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.rlt_item)
        RelativeLayout rlt_item;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;

        HomePageEnterpriseHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface EnterpriseClickInterface {
        void clickEnterprise(RxEinfos rxEinfos, int position);
    }

}
